import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';

import { Crisis, CrisisService } from './crisis.service';

@Injectable()
export class CrisisDetailResolverService implements Resolve<Crisis> {
    constructor(
        private cs: CrisisService,
        private router: Router
    ) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Crisis> {
        let id = route.paramMap.get('id');

        return this.cs.getCrisis(id).then(crisis => {
            if (crisis) {
                return crisis;
            } else {
                // id not found
                this.router.navigate(['/crisis-center']);
                return null;
            }
        });
    }
}